<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - code analysis - src/caffe/layers/filter_layer.cpp</title>
  <link rel="stylesheet" type="text/css" href="../../../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../../../index.html">top level</a> - <a href="index.html">src/caffe/layers</a> - filter_layer.cpp<span style="font-size: 80%;"> (source / <a href="filter_layer.cpp.func-sort-c.html">functions</a>)</span></td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">code analysis</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">2</td>
            <td class="headerCovTableEntry">61</td>
            <td class="headerCovTableEntryLo">3.3 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2020-09-11 22:25:26</td>
            <td></td>
            <td class="headerItem">Functions:</td>
            <td class="headerCovTableEntry">2</td>
            <td class="headerCovTableEntry">16</td>
            <td class="headerCovTableEntryLo">12.5 %</td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<a name="1"><span class="lineNum">       1 </span>            : #include &lt;vector&gt;</a>
<span class="lineNum">       2 </span>            : 
<span class="lineNum">       3 </span>            : #include &quot;caffe/layers/filter_layer.hpp&quot;
<span class="lineNum">       4 </span>            : #include &quot;caffe/util/math_functions.hpp&quot;
<span class="lineNum">       5 </span>            : 
<span class="lineNum">       6 </span>            : namespace caffe {
<span class="lineNum">       7 </span>            : 
<span class="lineNum">       8 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">       9 </span><span class="lineNoCov">          0 : void FilterLayer&lt;Dtype&gt;::LayerSetUp(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,</span>
<span class="lineNum">      10 </span>            :       const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">      11 </span><span class="lineNoCov">          0 :   CHECK_EQ(top.size(), bottom.size() - 1);</span>
<span class="lineNum">      12 </span><span class="lineNoCov">          0 :   first_reshape_ = true;</span>
<span class="lineNum">      13 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">      14 </span>            : 
<span class="lineNum">      15 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      16 </span><span class="lineNoCov">          0 : void FilterLayer&lt;Dtype&gt;::Reshape(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,</span>
<span class="lineNum">      17 </span>            :       const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">      18 </span>            :   // bottom[0...k-1] are the blobs to filter
<span class="lineNum">      19 </span>            :   // bottom[last] is the &quot;selector_blob&quot;
<span class="lineNum">      20 </span><span class="lineNoCov">          0 :   int selector_index = bottom.size() - 1;</span>
<span class="lineNum">      21 </span><span class="lineNoCov">          0 :   for (int i = 1; i &lt; bottom[selector_index]-&gt;num_axes(); ++i) {</span>
<span class="lineNum">      22 </span><span class="lineNoCov">          0 :     CHECK_EQ(bottom[selector_index]-&gt;shape(i), 1)</span>
<span class="lineNum">      23 </span>            :         &lt;&lt; &quot;Selector blob dimensions must be singletons (1), except the first&quot;;
<span class="lineNum">      24 </span>            :   }
<span class="lineNum">      25 </span><span class="lineNoCov">          0 :   for (int i = 0; i &lt; bottom.size() - 1; ++i) {</span>
<span class="lineNum">      26 </span><span class="lineNoCov">          0 :     CHECK_EQ(bottom[selector_index]-&gt;shape(0), bottom[i]-&gt;shape(0)) &lt;&lt;</span>
<span class="lineNum">      27 </span>            :         &quot;Each bottom should have the same 0th dimension as the selector blob&quot;;
<span class="lineNum">      28 </span>            :   }
<span class="lineNum">      29 </span>            : 
<span class="lineNum">      30 </span><span class="lineNoCov">          0 :   const Dtype* bottom_data_selector = bottom[selector_index]-&gt;cpu_data();</span>
<span class="lineNum">      31 </span>            :   indices_to_forward_.clear();
<span class="lineNum">      32 </span>            : 
<span class="lineNum">      33 </span>            :   // look for non-zero elements in bottom[0]. Items of each bottom that
<span class="lineNum">      34 </span>            :   // have the same index as the items in bottom[0] with value == non-zero
<span class="lineNum">      35 </span>            :   // will be forwarded
<span class="lineNum">      36 </span><span class="lineNoCov">          0 :   for (int item_id = 0; item_id &lt; bottom[selector_index]-&gt;shape(0); ++item_id) {</span>
<span class="lineNum">      37 </span>            :     // we don't need an offset because item size == 1
<span class="lineNum">      38 </span><span class="lineNoCov">          0 :     const Dtype* tmp_data_selector = bottom_data_selector + item_id;</span>
<span class="lineNum">      39 </span><span class="lineNoCov">          0 :     if (*tmp_data_selector) {</span>
<span class="lineNum">      40 </span><span class="lineNoCov">          0 :       indices_to_forward_.push_back(item_id);</span>
<span class="lineNum">      41 </span>            :     }
<span class="lineNum">      42 </span>            :   }
<span class="lineNum">      43 </span>            :   // only filtered items will be forwarded
<span class="lineNum">      44 </span><span class="lineNoCov">          0 :   int new_tops_num = indices_to_forward_.size();</span>
<span class="lineNum">      45 </span>            :   // init
<span class="lineNum">      46 </span><span class="lineNoCov">          0 :   if (first_reshape_) {</span>
<span class="lineNum">      47 </span><span class="lineNoCov">          0 :     new_tops_num = bottom[0]-&gt;shape(0);</span>
<span class="lineNum">      48 </span><span class="lineNoCov">          0 :     first_reshape_ = false;</span>
<span class="lineNum">      49 </span>            :   }
<span class="lineNum">      50 </span><span class="lineNoCov">          0 :   for (int t = 0; t &lt; top.size(); ++t) {</span>
<span class="lineNum">      51 </span><span class="lineNoCov">          0 :     int num_axes = bottom[t]-&gt;num_axes();</span>
<span class="lineNum">      52 </span><span class="lineNoCov">          0 :     vector&lt;int&gt; shape_top(num_axes);</span>
<span class="lineNum">      53 </span><span class="lineNoCov">          0 :     shape_top[0] = new_tops_num;</span>
<span class="lineNum">      54 </span><span class="lineNoCov">          0 :     for (int ts = 1; ts &lt; num_axes; ++ts)</span>
<span class="lineNum">      55 </span><span class="lineNoCov">          0 :       shape_top[ts] = bottom[t]-&gt;shape(ts);</span>
<span class="lineNum">      56 </span><span class="lineNoCov">          0 :     top[t]-&gt;Reshape(shape_top);</span>
<span class="lineNum">      57 </span>            :   }
<span class="lineNum">      58 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">      59 </span>            : 
<span class="lineNum">      60 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      61 </span><span class="lineNoCov">          0 : void FilterLayer&lt;Dtype&gt;::Forward_cpu(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,</span>
<span class="lineNum">      62 </span>            :       const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">      63 </span><span class="lineNoCov">          0 :   int new_tops_num = indices_to_forward_.size();</span>
<span class="lineNum">      64 </span>            :   // forward all filtered items for all bottoms but the Selector (bottom[last])
<span class="lineNum">      65 </span><span class="lineNoCov">          0 :   for (int t = 0; t &lt; top.size(); ++t) {</span>
<span class="lineNum">      66 </span><span class="lineNoCov">          0 :     const Dtype* bottom_data = bottom[t]-&gt;cpu_data();</span>
<span class="lineNum">      67 </span><span class="lineNoCov">          0 :     Dtype* top_data = top[t]-&gt;mutable_cpu_data();</span>
<span class="lineNum">      68 </span><span class="lineNoCov">          0 :     int dim = bottom[t]-&gt;count() / bottom[t]-&gt;shape(0);</span>
<span class="lineNum">      69 </span><span class="lineNoCov">          0 :     for (int n = 0; n &lt; new_tops_num; ++n) {</span>
<span class="lineNum">      70 </span><span class="lineNoCov">          0 :       int data_offset_top = n * dim;</span>
<span class="lineNum">      71 </span><span class="lineNoCov">          0 :       int data_offset_bottom = indices_to_forward_[n] * bottom[t]-&gt;count(1);</span>
<span class="lineNum">      72 </span><span class="lineNoCov">          0 :       caffe_copy(dim, bottom_data + data_offset_bottom,</span>
<span class="lineNum">      73 </span>            :           top_data + data_offset_top);
<span class="lineNum">      74 </span>            :     }
<span class="lineNum">      75 </span>            :   }
<span class="lineNum">      76 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">      77 </span>            : 
<span class="lineNum">      78 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      79 </span><span class="lineNoCov">          0 : void FilterLayer&lt;Dtype&gt;::Backward_cpu(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top,</span>
<span class="lineNum">      80 </span>            :       const vector&lt;bool&gt;&amp; propagate_down, const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom) {
<span class="lineNum">      81 </span><span class="lineNoCov">          0 :   if (propagate_down[bottom.size() - 1]) {</span>
<span class="lineNum">      82 </span><span class="lineNoCov">          0 :     LOG(FATAL) &lt;&lt; this-&gt;type()</span>
<span class="lineNum">      83 </span>            :                &lt;&lt; &quot;Layer cannot backpropagate to filter index inputs&quot;;
<span class="lineNum">      84 </span>            :   }
<span class="lineNum">      85 </span><span class="lineNoCov">          0 :   for (int i = 0; i &lt; top.size(); i++) {</span>
<span class="lineNum">      86 </span>            :     // bottom[last] is the selector and never needs backpropagation
<span class="lineNum">      87 </span>            :     // so we can iterate over top vector because top.size() == bottom.size() -1
<span class="lineNum">      88 </span><span class="lineNoCov">          0 :     if (propagate_down[i]) {</span>
<span class="lineNum">      89 </span><span class="lineNoCov">          0 :       const int dim = top[i]-&gt;count() / top[i]-&gt;shape(0);</span>
<span class="lineNum">      90 </span>            :       int next_to_backward_offset = 0;
<span class="lineNum">      91 </span>            :       int batch_offset = 0;
<span class="lineNum">      92 </span>            :       int data_offset_bottom = 0;
<span class="lineNum">      93 </span>            :       int data_offset_top = 0;
<span class="lineNum">      94 </span><span class="lineNoCov">          0 :       for (int n = 0; n &lt; bottom[i]-&gt;shape(0); n++) {</span>
<span class="lineNum">      95 </span><span class="lineNoCov">          0 :         data_offset_bottom = n * dim;</span>
<span class="lineNum">      96 </span><span class="lineNoCov">          0 :         if (next_to_backward_offset &gt;= indices_to_forward_.size()) {</span>
<span class="lineNum">      97 </span>            :           // we already visited all items that were been forwarded, so
<span class="lineNum">      98 </span>            :           // just set to zero remaining ones
<span class="lineNum">      99 </span><span class="lineNoCov">          0 :           caffe_set(dim, Dtype(0),</span>
<span class="lineNum">     100 </span><span class="lineNoCov">          0 :               bottom[i]-&gt;mutable_cpu_diff() + data_offset_bottom);</span>
<span class="lineNum">     101 </span>            :         } else {
<span class="lineNum">     102 </span><span class="lineNoCov">          0 :           batch_offset = indices_to_forward_[next_to_backward_offset];</span>
<span class="lineNum">     103 </span><span class="lineNoCov">          0 :           if (n != batch_offset) {  // this data was not been forwarded</span>
<span class="lineNum">     104 </span><span class="lineNoCov">          0 :             caffe_set(dim, Dtype(0),</span>
<span class="lineNum">     105 </span><span class="lineNoCov">          0 :                 bottom[i]-&gt;mutable_cpu_diff() + data_offset_bottom);</span>
<span class="lineNum">     106 </span>            :           } else {  // this data was been forwarded
<span class="lineNum">     107 </span><span class="lineNoCov">          0 :             data_offset_top = next_to_backward_offset * dim;</span>
<span class="lineNum">     108 </span><span class="lineNoCov">          0 :             next_to_backward_offset++;  // point to next forwarded item index</span>
<span class="lineNum">     109 </span><span class="lineNoCov">          0 :             caffe_copy(dim, top[i]-&gt;mutable_cpu_diff() + data_offset_top,</span>
<span class="lineNum">     110 </span><span class="lineNoCov">          0 :                 bottom[i]-&gt;mutable_cpu_diff() + data_offset_bottom);</span>
<span class="lineNum">     111 </span>            :           }
<span class="lineNum">     112 </span>            :         }
<span class="lineNum">     113 </span>            :       }
<span class="lineNum">     114 </span>            :     }
<span class="lineNum">     115 </span>            :   }
<span class="lineNum">     116 </span><span class="lineNoCov">          0 : }</span>
<a name="117"><span class="lineNum">     117 </span>            : </a>
<span class="lineNum">     118 </span>            : #ifdef CPU_ONLY
<span class="lineNum">     119 </span><span class="lineNoCov">          0 : STUB_GPU(FilterLayer);</span>
<span class="lineNum">     120 </span>            : #endif
<span class="lineNum">     121 </span>            : 
<span class="lineNum">     122 </span>            : INSTANTIATE_CLASS(FilterLayer);
<a name="123"><span class="lineNum">     123 </span><span class="lineCov">          3 : REGISTER_LAYER_CLASS(Filter);</span></a>
<span class="lineNum">     124 </span>            : 
<span class="lineNum">     125 </span><span class="lineCov">          3 : }  // namespace caffe</span>
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.12</a></td></tr>
  </table>
  <br>

</body>
</html>
